<?php
require_once "class.rglParser.php";

function getVarAbilCalcDato($idSetDati,$codDato) {
	global $objDB;	
	$aVA = array();
	$aTipiTD[1][1] = array('Q');
	$aTipiTD[2][1] = array('B','C');
	$aTipiTD[2][2] = array('Q');
	$aTipiTD[3][1] = array('B','C');
	$aTipiTD[3][2] = array('Q','P');
	$aTipiTD[3][3] = array('Q');
	$aTipiTD[11][1] = array('B','C');
	$aTipiTD[11][2] = array('Q','P');
	$aTipiTD[11][3] = array('Q');
	$aTipiTD[11][11] = array('Q');
	$aTipiTD[12][1] = array('B','C');
	$aTipiTD[12][2] = array('Q','P');
	$aTipiTD[12][3] = array('Q');
	$aTipiTD[12][12] = array('Q');

	$sqlSel = "SELECT d.cod_dato, d.tipo_tab_dati FROM rgl_def_dato d "
			." WHERE d.id_set_dati = ".$idSetDati." AND d.cod_dato = '$codDato'";
	$objDB->query($sqlSel,'selRecVA');
	$acDA=$objDB->fetch('selRecVA');
	$tipoTDris =  $acDA['tipo_tab_dati'];
	
	$sqlSel = "SELECT d.cod_dato, d.tipo_tab_dati FROM rgl_def_dato d "
			." WHERE d.id_set_dati = ".$idSetDati;
	$objDB->query($sqlSel,'selRecVA');
	while ($acDA=$objDB->fetch('selRecVA')) {
		$tipoTDinp = $acDA['tipo_tab_dati'];
		if (isset($aTipiTD[$tipoTDris][$tipoTDinp])) {
			foreach($aTipiTD[$tipoTDris][$tipoTDinp] as $prefX) {
				$aVA[$prefX.'_'.$acDA['cod_dato']] = 'non-modif';
			}
		}
	}
	$aVA['Q_'.$codDato] = 'modificabile';
	return $aVA;
}

function getGerarcDefDati($aDefDati, $tipoTabDati) {
	global $objDB;
	$aDati = array();
	$aFigli = array();
	$aGeDati = array();
	foreach($aDefDati as $codX => $aRec) {
		if ($aRec['tipo_tab_dati'] != $tipoTabDati) continue;		
		$aDati[$aRec['cod_dato']] = $aRec;
		if ($aRec['cod_dato_padre'] == '') {			
			$aGeDati[$aRec['cod_dato']] = array();
		} else {
			$aFigli[$aRec['cod_dato_padre']][$aRec['cod_dato']] = $aRec;
		}
	}
	//dbg_($aFigli);
	foreach($aGeDati as $codX => $aX) {
		$aGeDati[$codX] = caricaFigli($codX,$aFigli,$aDati,0);
	}
	return $aGeDati;
}

function getGerarcTabDati($idSetDati, $tipoTabDati) {
	global $objDB;
	$sSql = "SELECT d.* FROM rgl_def_dato d "
		." WHERE d.id_set_dati = ".intval($idSetDati)
		." AND d.tipo_tab_dati = ".intval($tipoTabDati)
		." ORDER BY d.seq ";
	//dbg_($sSql);
	$objDB->query($sSql,'geraTdati');
	$aDati = array();
	$aFigli = array();
	$aGeDati = array();
	while($aRec = $objDB->fetch('geraTdati')) {		
		$aDati[$aRec['cod_dato']] = $aRec;
		if ($aRec['cod_dato_padre'] == '') {			
			$aGeDati[$aRec['cod_dato']] = array();
		} else {
			$aFigli[$aRec['cod_dato_padre']][$aRec['cod_dato']] = $aRec;
		}
	}
	//dbg_($aFigli);
	foreach($aGeDati as $codX => $aX) {
		$aGeDati[$codX] = caricaFigli($codX,$aFigli,$aDati,0);
	}
	return $aGeDati;
}

function caricaFigli($codX,&$aFigli,&$aDati,$liv) {	
	//dbg_($codX." / ".$liv." caricaFigli");
	$aX['attr'] = $aDati[$codX];
	$aX['figli'] = array();
	if ($liv > 20) {
		$aX['attr']['descr'] = "+ di 20 livelli !!!";
		return $aX;
	}
	if (isset($aFigli[$codX])) {
		foreach($aFigli[$codX] as $codF => $aRec) {
			$aX['figli'][$codF] = caricaFigli($codF,$aFigli,$aDati,$liv+1);
		}
	}
	//dbg_(str_repeat('.',$liv).$codX." figli=".implode(',',array_keys($aX['figli'])));
	return $aX;
}
function aVarLoadBil($idBil,&$aVar, &$aDefDati, $alias='B',$bAnnualizza=false) {
	global $objDB, $objSess;
	$sSql = "SELECT b.id_set_dati, b.str_def_dati, b.str_dati, b.data_rif, b.data_iniz, b.fonte_bil"
		.", b.descr AS descr_bil, b.nota AS nota_bil, b.stato_lav"
		.", sd.cod_set_dati, a.*, s.descr AS descr_sett "
		." FROM rgl_bilancio b "
		." INNER JOIN rgl_azienda a ON a.id_azienda = b.id_azienda "
		." LEFT JOIN rgl_settore s ON s.cod_sett = a.cod_sett "
		." INNER JOIN rgl_set_dati sd ON sd.id_set_dati = b.id_set_dati "
		." WHERE b.id_bil = ".intval($idBil);
	$objDB->query($sSql, 'avrld');
	$aRec = $objDB->fetch('avrld');
	if (!$aRec) {
		return 'no bil. '.$idBil;
	}
	foreach($aRec as $dX => $vX) {
		$aVar[$alias.'_'.strtoupper($dX)] = $vX;
	}
	$mesi = round((strtotime($aRec['data_rif'])-strtotime($aRec['data_iniz']))/(3600*24*30));
	$aVar['B_MESI_BIL'] = $mesi;
	$coeff = 12 / $mesi;
	$aDefDati = unsgzu($aRec['str_def_dati']);
	$aDati = unsgzu($aRec['str_dati']);
	foreach($aDati as $dX => $vX) {
		if ($bAnnualizza && $mesi != 12 && $aDefDati[strtoupper($dX)]['situaz_progr']=='P') {
			$aDefDati[strtoupper($dX)]['annualizzato'] = true;
			$vX = $coeff * $vX;
		}
		$aVar[$alias.'_'.strtoupper($dX)] = $vX;
	}
	return '';
}
		
function aVarLoadAnag($idBil,&$aVar,$alias='A') {
	global $objDB, $objSess;
	$sSql = "SELECT a.* "
		." FROM rgl_bilancio b "
		." INNER JOIN rgl_azienda a ON a.id_azienda = b.id_azienda "
		." WHERE b.id_bil = ".intval($idBil);
	$objDB->query($sSql, 'avrld');
	$aRec = $objDB->fetch('avrld');
	if (!$aRec) {
		return 'no bil. '.$idBil;
	}
	foreach($aRec as $dX => $vX) {
		$aVar[$alias.'_'.strtoupper($dX)] = $vX;
	}
	return '';
}

function aVarCalcTabDati($idBil,$idBilP,&$aVar,&$aDefDati,$tipoTabDati) {
	global $objDB, $objSess;
	$aVar = array();
	$aDefDati = array();
	$msg = aVarLoadBil($idBil,$aVar,$aDefDati,'B',true);
	if ($msg!='') {
		return $msg;
	}
	$idSetdati = $aVar['B_ID_SET_DATI'];
	$codSetDati = $aVar['B_COD_SET_DATI'];
	if ($idBilP > 0) {
		$aDefDatiP = array();
		$msg = aVarLoadBil($idBilP,$aVar,$aDefDatiP,'C',true);
	}
	if ($msg!='' || $tipoTabDati < 2) {
		return $msg;
	}
	//crea array $aVarP per bil. anno prec.$msgPag .= "*** QUADRA ***";
	$aVarP = array();
	foreach($aVar as $cX => $vX) {
		if ($cX[0]=='C') {
			$aVarP['B'.substr($cX,1)] = $vX;
		}
	}
	//calcola bil.ric anno corr e prec
	foreach($aDefDati as $kX => $acX) {
		if ($acX['tipo_tab_dati']!=2
		|| $acX['script_php']=='') {
			continue;
		}
		$aVar['Q_'.$acX['cod_dato']] = 0;
		$aVarP['Q_'.$acX['cod_dato']] = 0;
		$nomeFunz = "rglScrDato".$acX['cod_dato'];
		if (!function_exists($nomeFunz)) {
			eval($acX['script_php']);
			if (!function_exists($nomeFunz)) {
				return "Script $codSetDati.$nomeFunz anomalo";
			}
		}
		$aVar["_SCRIPT_MESS"] = '';
		$rc = $nomeFunz($aVar); //anno corr.
		$rc = $nomeFunz($aVarP); //anno prec
	}
	//carica anno prec in aVar conb pref. P
	foreach($aVarP as $cX => $vX) {
		if ($cX[0]=='Q') {
			$aVar['P'.substr($cX,1)] = $vX;
		}
	}
	if ($msg!='' || $tipoTabDati < 3) {
		return $msg;
	}
	
	if ($tipoTabDati>10) { //calcolo indici - tutti i calcoli
		$tipoTabDati = 99; 
	} 
	foreach($aDefDati as $kX => $acX) {
		if (($acX['tipo_tab_dati'] < 3 && $acX['tipo_tab_dati'] > $tipoTabDati)
		|| ($acX['script']=='' && $acX['flag_quad_calc']!='totsub')) {
			continue;
		}
		$aVar['Q_'.$acX['cod_dato']] = 0;
		$nomeFunz = "rglScrDato".$acX['cod_dato'];
		if (!function_exists($nomeFunz)) {
			eval($acX['script_php']);
			if (!function_exists($nomeFunz)) {
				return "Script $codSetDati.$nomeFunz anomalo";
			}
		}
		$aVar["_SCRIPT_MESS"] = '';
		$rc = $nomeFunz($aVar);
	}
	return $msg;	
}
